//
//  BIDSetFontSizeViewController.m
//  shangwuting
//
//  Created by mal on 14-1-6.
//  Copyright (c) 2014年 mal. All rights reserved.
//

#import "BIDSetFontSizeViewController.h"
#import "BIDOneRowCell.h"

extern int const SMALL_FONT_SIZE ;
extern int const MIDDLE_FONT_SIZE ;
extern int const BIG_FONT_SIZE ;

@interface BIDSetFontSizeViewController ()

@end

@implementation BIDSetFontSizeViewController
@synthesize myTableView;
@synthesize bRegiser;
@synthesize fontSize;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"字体设置";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    bRegiser = NO;
    self.fontSize = [AppDelegate getFontSize];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    myTableView = nil;
}

- (void)backBtnHandler
{
    //退出之前先保存设置的字体大小
    [AppDelegate setFontSize:fontSize];
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:[NSNumber numberWithInt:fontSize] forKey:@"font"];
    [userDefaults synchronize];
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"identifier";
    if(!bRegiser)
    {
        UINib *nib = [UINib nibWithNibName:@"BIDOneRowCell" bundle:nil];
        [tableView registerNib:nib forCellReuseIdentifier:identifier];
    }
    BIDOneRowCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    switch([indexPath row])
    {
        case 0:
        {
            //小字体
            cell.label1.text = @"小";
            cell.label2.font = [UIFont systemFontOfSize:SMALL_FONT_SIZE];
            if([AppDelegate getFontSize]==SMALL_FONT_SIZE)
                [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
            break;
        case 1:
        {
            //中号字体
            cell.label1.text = @"中(默认)";
            cell.label2.font = [UIFont systemFontOfSize:MIDDLE_FONT_SIZE];
            if([AppDelegate getFontSize]==MIDDLE_FONT_SIZE)
                [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
            break;
        case 2:
        {
            //大号字体
            cell.label1.text = @"大";
            cell.label2.font = [UIFont systemFontOfSize:BIG_FONT_SIZE];
            if([AppDelegate getFontSize]==BIG_FONT_SIZE)
                [tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
        }
            break;
    }
    cell.label2.text = @"快乐每一天";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    switch([indexPath row])
    {
        case 0:
        {
            fontSize = SMALL_FONT_SIZE;
        }
            break;
        case 1:
        {
            fontSize = MIDDLE_FONT_SIZE;
        }
            break;
        case 2:
        {
            fontSize = BIG_FONT_SIZE;
        }
            break;
    }
}

@end
